import Opt from '@/components/dt/info/Opt.js'
import nameOpt from '@/biz/option/name.js'
import billMgr from '@/biz/bonus/bill'
import dt from '@dt/dt'

class RoomPersonColumn extends Opt {

  constructor(name, type, isParticipate, change) {
    super()
    this.name = name
    this.type = type
    this.isParticipate = isParticipate
    this.change = change
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {

    this.userName = {
      field: 'userName',
      title: '姓名',
      minWidth: 110,
      fixed: 'left',
      cell: (item) => {
        if (item.isNeedReplace && this.name == 'billEdit') {
          let name = nameOpt.create({
            title: '姓名',
            disabled: !this.isParticipate,
            options: [{
              key: item.userId,
              value: item.userName,
              workNo: item.workNo
            }],
            data: {
              from: (item) => {
                return item.userId
              },
              to: (val) => {
                let opt = name.select.options.find(i => i.key == val.value)
                return {
                  workNo: opt.workNo,
                  userName: opt.value,
                  userId: opt.key
                }
              }
            },
            change: ({ info }) => {
              billMgr.roomPersonChange(info.bonusDetailId, info.userId, info
                .userName, info.workNo)
            }
          })
          return {
            edit: name
          }
        } else {
          return {
            text: {}
          }
        }
      }
    }
    
    this.all = [{
        type: 'index',
        title: '序号',
        minWidth: 50,
        fixed: 'left'
      },
      this.userName,
      {
        field: 'workNo',
        title: '工号',
        minWidth: 90,
        cell: {
          text: { show: true }
        }
      },
      {
        field: 'jobStatusStr',
        title: '在职情况',
        minWidth: 90,
        cell: {
          text: { show: true }
        }
      },
      {
        field: 'postName',
        title: '岗位角色',
        minWidth: 110,
        cell: {
          value: (item) => {
            return item.isNeedReplace ? '-' : item.postName
          },
          text: {}
        }
      },
      {
        field: 'rankName',
        title: '职级',
        minWidth: 140,
        cell: {
          value: (item) => {
            return item.isNeedReplace ? '-' : item.rankName
          },
          text: {}
        }
      },
      {
        field: 'remind',
        title: '提点(万分之)',
        minWidth: 120,
      },
      {
        field: 'shareRadio',
        title: '分摊占比(%)',
        minWidth: 120,
      },
      {
        field: 'issueType',
        title: '发放类型',
        minWidth: 90,
      },
      {
        field: 'preBonusAmount',
        title: '预发奖金(元)',
        visible: this.type != 'PRE' && this.type != 'HANDOVER',
        minWidth: 130,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'bonusAmount',
        title: '结算奖金(元)',
        minWidth: 130,
        cell: {
          text: {
            type: 'number'
          }
        }
      },
      {
        field: 'adjustBonus',
        title: '调整金额(元)',
        minWidth: 130,
        cell: (info) => {
          if (this.name == 'billEdit' && info.isCanAdjust != false) {
            return {
              edit: {
                disabled: !this.isParticipate,
                input: {
                  type: 'number',
                  focus: (val) => {
                    this.initData = val
                  },
                  blur: () => {
                    if (info.adjustBonus != this.initData) this.change(info)
                  }
                }
              }
            }
          } else {
            return {
              text: {
                type: 'number'
              }
            }
          }
        }
      },
      {
        field: 'shouldBonusAmount',
        title: '应付奖金(元)',
        minWidth: 130,
        cell: {
          text: {
            type: 'number',
            show: true
          }
        }
      },
      {
        field: 'bonusPool',
        title: '计提奖金池(元)',
        minWidth: 130,
        cell: {
          text: {
            type: 'number',
            show: true
          }
        }
      },
      {
        field: 'actualBonusAmount',
        title: '实发奖金(元)',
        minWidth: 130,
        cell: {
          text: {
            type: 'number',
            show: true
          }
        }
      },
      {
        field: 'adjustReason',
        title: '调整原因',
        minWidth: 150,
        cell: (info) => {
          if (this.name == 'billEdit' && info.isCanAdjust != false) {
            return {
              edit: {
                disabled: !this.isParticipate,
                placeholder: '请输入调整原因',
                input: {
                  focus: (val) => {
                    this.initData = val
                  },
                  blur: () => {
                    if (info.adjustReason != this.initData) this.change(info)
                  }
                }
              }
            }
          } else {
            return {
              text: {}
            }
          }
        }
      },
      {
        title: '计算公式',
        width: 120,
        fixed: 'right',
        cell: (row) => {
          return {
            value: () => '查看计算公式',
            text: {
              color: '#3366ff',
              tooltip: true,
              tipsTxt: row.calProcess || '--'
            }
          }
        }
      }
    ]
  }
}

export default RoomPersonColumn
